<?php

class GalactusViewAccount extends GalactusView {

	function __construct($control) {
		parent::__construct($control);
	}

	public function retrieveJS( ) {
		return '
			function galactus_view_account_show( ) {
				$("#checkbox-all-users").click(function( ) {
					if($(this).is(":checked"))
						$(".params:checkbox").prop("checked", true);
					else $(".params:checkbox").prop("checked", false);
				});

				$(".pagination a").click(function( ) {
					var cmd = "'.URL.'" + $(this).attr("galactus-command");
					$("#contentarea").load(cmd, $(".params").serializeArray( ));
				});
			}
		';
	}

	public function show($command, $post) {
		switch($command[2]) {
			case "ADD": $this->showAdd( ); break;
			case "CHANGE_PROFILE": $this->showChangeProfile($post); break;
			default: $this->showDefault( ); break;
		}
	}

	private function showAdd( ) {
		$options = "";
		$profiles = $this->control->model->retrieveProfiles( );
		for($i = 0; $i < count($profiles); ++$i) {
			$options .= '<option value="'.$profiles[$i]["id"].'">'.$profiles[$i]["label"].'</option>';
		}
		echo '
			<form method="post" action="'.URL.'ACCOUNT/ADD" class="form-horizontal offset2">
				<div class="control-group">
					<label class="control-label" for="name">Nome</label>
					<div class="controls">
						<input type="text" name="params[ ]" class="span4" id="name"/>
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="profile">Perfil</label>
					<div class="controls">
						<select name="params[ ]" class="span4" id="profile">'.$options.'</select>
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="email">E-mail</label>
					<div class="controls">
						<input type="email" name="params[ ]" class="span4" id="email"/>
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="password">Senha</label>
					<div class="controls">
						<input type="password" name="params[ ]" class="span4" id="password" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="confirm-password">Confirmação de senha</label>
					<div class="controls">
						<input type="password" name="params[ ]" class="span4" id="confirm-password" />
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" for="account">Conta</label>
					<div class="controls">
						<label class="inline radio">
							<input type="radio" name="params[ ]" class="params" value="1"> Ativada
						</label>
						<label class="inline radio">
							<input type="radio" name="params[ ]" class="params" value=""> Desativada
						</label>
					</div>
				</div>
				<div class="control-group">
					<div class="controls">
						<button type="reset" class="btn span2">Limpar</button>
						<button type="submit" class="btn btn-primary span2">Confirmar</button>
					</div>
				</div>
			</form>
			<script type="text/javascript"> $(galactus_view_account_show_add_edit( )); </script>
		';
	}

	private function showChangeProfile($user_ids) {
		$hiddens = "";
		for($i = 0; $i < count($user_ids); ++$i) {
			$hiddens .= '<input type="hidden" name="params[ ]" value="'.$user_ids[$i].'" />';
		}
		$options = "";
		$profiles = $this->control->model->retrieveProfiles( );
		for($i = 0; $i < count($profiles); ++$i) {
			$options .= '<option value="'.$profiles[$i]["id"].'">'.$profiles[$i]["label"].'</option>';
		}
		echo '
			<form method="post" action="'.URL.'ACCOUNT/CHANGE_PROFILE" class="form-horizontal offset2">
				<div class="control-group">
					<label class="control-label" for="profile">Perfil</label>
					<div class="controls">
						<select name="params[ ]" class="span4" id="profile">'.$options.'</select>
					</div>
				</div>
				'.$hiddens.'
				<div class="control-group">
					<div class="controls">
						<button type="reset" class="btn span2">Limpar</button>
						<button type="submit" class="btn btn-primary span2">Confirmar</button>
					</div>
				</div>
			</form>
		';
	}

	public function showDefault( ) {
		$users = $this->control->model->retrieveUser( );
		$content = '';
		for($i = 0; $i < count($users); ++$i) {
			$date = new DateTime($users[$i]["created_on"]);
			$content .= '
				<tr>
					<td><input type="checkbox" name="params[ ]" class="params" value="'.$users[$i]["user_id"].'" /></td>
					<td>'.$users[$i]["name"].'</td>
					<td>'.$users[$i]["email"].'</td>
					<td>'.$users[$i]["profile"].'</td>
					<td>'.$date->format("d/m/Y").'</td>
					<td>'.(($users[$i]["active"])? 'Sim' : 'Não').'</td>
				</tr>
			';
		}
		echo '
			<table class="table table-condensed table-striped" id="table-users">
				<thead>
					<tr>
						<th><input type="checkbox" id="checkbox-all-users" /></th>
						<th>Nome</th>
						<th>E-mail</th>
						<th>Perfil</th>
						<th>Criado em</th>
						<th>Ativo</th>
					</tr>
				</thead>
				<tbody>'.$content.'</tbody>
			</table>
			<div class="pagination pagination-centered">
				<ul>
					<li><a galactus-command="ACCOUNT/SHOW/ADD" title="Adiciona um usuário"><i class="icon-plus-sign"></i></a></li>
					<li><a galactus-command="ACCOUNT/SHOW/CHANGE_PROFILE" title="Altera o perfil dos usuários selecionados"><i class="icon-user"></i></a></li>
					<li><a galactus-command="ACCOUNT/CHANGE_PASSWORD" title="Redefine a senha dos usuários selecionados"><i class="icon-cog"></i></a></li>
					<li><a galactus-command="ACCOUNT/REMOVE" title="Remove os usuários selecionados"><i class="icon-minus"></i></a></li>
				</ul>
			</div>
			<script type="text/javascript">$(galactus_view_account_show( ));</script>
		';
	}

}
?>
